package com.optionexplorer.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {

	public static SimpleDateFormat sdf = new SimpleDateFormat("MMMMM-yy");
	
	/**
	 * Sets the date on the calendar to the third friday of the month
	 * @param cal
	 * @return
	 */
	public static Calendar setCalToThirdFriday(Calendar cal) {
		
		//first spot the first friday of the month
		int ffIndex = 1;
		
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			ffIndex = 19;
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
			ffIndex = 18;
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
			ffIndex = 17;
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
			ffIndex = 16;
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
			ffIndex = 15;
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
			ffIndex = 21;			
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			ffIndex = 20;
		}

		cal.set(Calendar.DAY_OF_MONTH, ffIndex);			
		return cal;
	}
	
	
	/**
	 * Creates a data object for the month from the options page 
	 * e.g., Jan 11 is translated to a Date object in Jan 2011
	 * @param str
	 * @return
	 */
	public static Calendar getCalFromStr(String str) {
		
		String monthStr = str.substring(0,3);
		String yearStr = str.substring(4,6);
		Calendar cal = Calendar.getInstance();
		
		try {
			int yrNum = 2000 + Integer.parseInt(yearStr);
			cal.set(Calendar.YEAR, yrNum);
			cal.set(Calendar.DATE, 1);
			
			if (StringUtils.areStringsEqual(monthStr, "Jan")) {
				cal.set(Calendar.MONTH, Calendar.JANUARY);
			} else if (StringUtils.areStringsEqual(monthStr, "Feb")) {
				cal.set(Calendar.MONTH, Calendar.FEBRUARY);
			} else if (StringUtils.areStringsEqual(monthStr, "Mar")) {
				cal.set(Calendar.MONTH, Calendar.MARCH);
			} else if (StringUtils.areStringsEqual(monthStr, "Apr")) {
				cal.set(Calendar.MONTH, Calendar.APRIL);
			} else if (StringUtils.areStringsEqual(monthStr, "May")) {
				cal.set(Calendar.MONTH, Calendar.MAY);
			} else if (StringUtils.areStringsEqual(monthStr, "Jun")) {
				cal.set(Calendar.MONTH, Calendar.JUNE);
			} else if (StringUtils.areStringsEqual(monthStr, "Jul")) {
				cal.set(Calendar.MONTH, Calendar.JULY);
			} else if (StringUtils.areStringsEqual(monthStr, "Aug")) {
				cal.set(Calendar.MONTH, Calendar.AUGUST);
			} else if (StringUtils.areStringsEqual(monthStr, "Sep")) {
				cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
			} else if (StringUtils.areStringsEqual(monthStr, "Oct")) {
				cal.set(Calendar.MONTH, Calendar.OCTOBER);
			} else if (StringUtils.areStringsEqual(monthStr, "Nov")) {
				cal.set(Calendar.MONTH, Calendar.NOVEMBER);
			} else if (StringUtils.areStringsEqual(monthStr, "Dec")) {
				cal.set(Calendar.MONTH, Calendar.DECEMBER);
			}						
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}	
		return cal;		
	}
	
	
	public static String formatSimple(Date d) {
		String str = null;
		str = sdf.format(d);
		return str;
	}
	
	/**
	 * Returns the days remaining from today to the argument date 
	 * @param d
	 * @return
	 */
	public static int getDaysRemaining(Date d) {
		
		int remDays = 0;
		Calendar todayCal = Calendar.getInstance();
		if (d.before(todayCal.getTime())) {
			return remDays;
		}
		
		Calendar targetCal = Calendar.getInstance();
		targetCal.setTime(d);
		
		remDays = targetCal.get(Calendar.DAY_OF_YEAR) - todayCal.get(Calendar.DAY_OF_YEAR);
		
		if (targetCal.get(Calendar.YEAR) > todayCal.get(Calendar.YEAR)) {
			remDays = remDays + ((targetCal.get(Calendar.YEAR) - todayCal.get(Calendar.YEAR))*365);
		}
		
		return remDays;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = null;
		
		Calendar cal1 = Calendar.getInstance();
		cal1.add(Calendar.DAY_OF_YEAR, -15);
		cal1.add(Calendar.YEAR,1);
		System.out.println("Days Remaining : " + getDaysRemaining(cal1.getTime()));
		
		
		Calendar cal = Calendar.getInstance();
		System.out.println(formatSimple(cal.getTime()));
		
		str = "Apr 09";
		System.out.println("Third Friday : " + setCalToThirdFriday(getCalFromStr(str)).getTime());
		str = "Jan 09";
		System.out.println("Third Friday : " + setCalToThirdFriday(getCalFromStr(str)).getTime());
		str = "Aug 10";
		System.out.println("Third Friday : " + setCalToThirdFriday(getCalFromStr(str)).getTime());
		str = "Dec 11";
		System.out.println("Third Friday : " + setCalToThirdFriday(getCalFromStr(str)).getTime());
		str = "Jun 10";
		System.out.println("Third Friday : " + setCalToThirdFriday(getCalFromStr(str)).getTime());

	}

}
